package com.dsg.service.setting.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.dao.setting.ProfunMapper;
import com.dsg.entity.setting.Profun;
import com.dsg.service.base.impl.BaseServiceImpl;
import com.dsg.service.setting.IProfunSerivce;
import com.dsg.util.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("profunSerivce")
public class ProfunSerivceImpl extends BaseServiceImpl<ProfunMapper, Profun> implements IProfunSerivce {

    @Autowired
    private ProfunMapper profunMapper;

    @Override
    public Page<Profun> selectByList(Page<Profun> page, Profun profun) {
        page.setRecords(profunMapper.selectByList(page, profun));
        return page;
    }

    @Override
    public List<Profun> selectByProjectCode(String code) {
        return profunMapper.selectByProjectCode(code);
    }

    @Override
    public Profun selectOneByProjectCode(String code) {
        return profunMapper.selectOneByProjectCode(code);
    }

    @Override
    public void deleteProfun(String code) {
        if (StringUtil.isEmpty(code)) {
            throw new RRException("功能代碼參數不能為空!");
        }
        List<Profun> proFunList = profunMapper.selectProfunList(code);
        if (CollectionUtil.isNotEmpty(proFunList)) {
            List<String> oidList = proFunList.stream().map(Profun::getOid).collect(Collectors.toList());
            profunMapper.deleteBatchIds(oidList);
        }
    }
}
